package com.dc.web.controller;


import com.dc.constant.Constants;
import com.dc.constant.MessageConstant;
import com.dc.dao.*;
import com.dc.entity.*;
import com.dc.exception.ServiceException;
import com.dc.model.AjaxResult;
import com.dc.model.WorkflowListByShipRepairBody;
import com.dc.service.WorkflowStatService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@Api(tags = "单船工序统计界面")
@RequestMapping("/api/workflow/stat")
@RestController
public class WorkflowStatController {

    @Resource
    ShipDao shipDao;

    @Resource
    ShipRepairDao shipRepairDao;

    @Resource
    WorkflowTopThirdDao thirdDao;

    @Resource
    WorkflowTopSecondDao secondDao;

    @Resource
    WorkflowTopFirstDao firstDao;

    @Resource
    WorkflowStatService workflowStatService;

    /**
     * 如果是竣工的，直接查询
     * 如果不是竣工的，则计算
     * @param body
     * @return
     */
    @GetMapping("/third")
    public AjaxResult getThirdList(WorkflowListByShipRepairBody body) {
        Long repairId = body.getRepairId();
        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(repairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepair = optionalShipRepair.get();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipRepair.getShipId());
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        Integer complete = shipRepair.getComplete();

        // 竣工, 直接查询
        if(Constants.SHIP_REPAIR_STAT_COMPLETE.equals(complete)) {
            List<WorkflowTopThirdEntity> list = thirdDao.findByShipRepairEntity(shipRepair);
            return AjaxResult.success(list);
        }

        // 没有竣工，计算 TODO
        List<WorkflowTopThirdEntity> topThird = workflowStatService.getTopThird(ship, shipRepair);
        return AjaxResult.success(topThird);
    }

    /**
     * 如果是竣工的，直接查询
     * 如果不是竣工的，则计算
     * @param body
     * @return
     */
    @GetMapping("/second")
    public AjaxResult getSecondList(WorkflowListByShipRepairBody body) {
        Long repairId = body.getRepairId();
        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(repairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepair = optionalShipRepair.get();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipRepair.getShipId());
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        Integer complete = shipRepair.getComplete();

        // 竣工, 直接查询
        if(Constants.SHIP_REPAIR_STAT_COMPLETE.equals(complete)) {
            List<WorkflowTopSecondEntity> list = secondDao.findByShipRepairEntity(shipRepair);
            return AjaxResult.success(list);
        }

        // = null，则没有竣工，计算 TODO
        List<WorkflowTopSecondEntity> topSecond = workflowStatService.getTopSecond(ship, shipRepair);
        return AjaxResult.success(topSecond);
    }


    /**
     * 如果是竣工的，直接查询
     * 如果不是竣工的，则计算
     * @param body
     * @return
     */
    @GetMapping("/first")
    public AjaxResult getFirstList(WorkflowListByShipRepairBody body) {
        Long repairId = body.getRepairId();
        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(repairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepair = optionalShipRepair.get();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipRepair.getShipId());
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        Integer complete = shipRepair.getComplete();

        // 竣工, 直接查询
        if(Constants.SHIP_REPAIR_STAT_COMPLETE.equals(complete)) {
            List<WorkflowTopFirstEntity> list = firstDao.findByShipRepairEntity(shipRepair);
            double sumCShen = list.stream().mapToDouble(WorkflowTopFirstEntity::getCShen).sum();
            double sumCOcean = list.stream().mapToDouble(WorkflowTopFirstEntity::getCOcean).sum();
            Sum sum = new Sum(sumCShen, sumCOcean);
            Map<String, Object> result = new HashMap<>();
            result.put("list", list);
            result.put("sum", sum);
            return AjaxResult.success(result);
        }

        // 没有竣工，计算 TODO
        List<WorkflowTopFirstEntity> topFirst = workflowStatService.getTopFirst(ship, shipRepair);
        double sumCShen = topFirst.stream().mapToDouble(WorkflowTopFirstEntity::getCShen).sum();
        double sumCOcean = topFirst.stream().mapToDouble(WorkflowTopFirstEntity::getCOcean).sum();
        Sum sum = new Sum(sumCShen, sumCOcean);
        Map<String, Object> result = new HashMap<>();
        result.put("list", topFirst);
        result.put("sum", sum);
        return AjaxResult.success(result);
    }

    @Data
    @AllArgsConstructor
    static class Sum {
        public double sumCShen;
        public double sumCOcean;
    }
}
